package com.sss.myspringsecurity.config.securityconfig.handler;

import com.alibaba.fastjson.JSON;
import com.sss.myspringsecurity.util.ResponseDataEnum;
import com.sss.myspringsecurity.util.Result;
import com.sss.myspringsecurity.util.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 匿名未登录的时候访问,需要登录的资源的调用类
 * @author sss
 * @date 2020-02-09 19:33
 **/
@Component
public class CustomerAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        ServletUtils.renderString(httpServletResponse, JSON.toJSONString(Result.fail(ResponseDataEnum.NOTUSER)));
    }
}
